﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ConnectToDatabase;
using ConnectToDatabase.Model;

namespace QLGiayDep.Forms
{
    /// <summary>
    /// Interaction logic for FormTraCuuSanPham.xaml
    /// </summary>
    public partial class FormTraCuuSanPham : Page
    {
        SelectDB selectDB;
        InsertDB insertDB;
        SetID setID;
        int m_iFrom = 0;
        int m_iTo = 14;
        int m_iDistance = 0;
        static bool m_bSelectIndex = false;
        string URL;

        public FormTraCuuSanPham(string _URL)
        {
            InitializeComponent();
            URL = _URL;
            setID = (SetID)Activator.GetObject(typeof(SetID), URL + "SETID_OBJECT");
            insertDB = (InsertDB)Activator.GetObject(typeof(InsertDB), URL + "INSERTDB_OBJECT");
            selectDB = (SelectDB)Activator.GetObject(typeof(SelectDB), URL + "SELECTDB_OBJECT");
            m_iDistance = m_iTo - m_iFrom + 1;
            AddComboboxPagingTraCuu(cmdPagingTraCuu, selectDB.SelectAllDSSanPham().Count);
            AddComboboxTimTheo();

            m_iDistance = m_iTo - m_iFrom + 1;
            if (m_iFrom + m_iDistance > selectDB.SelectAllDSSanPham().Count)
                dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham();
            else
                dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham(m_iFrom, m_iTo);
        }

        private void AddComboboxPagingTraCuu(ComboBox m_cmbTemp, int m_iCountTemp)
        {
            m_cmbTemp.Items.Clear();
            int count = m_iCountTemp;

            int temp = count / m_iDistance;
            if (temp * m_iDistance < count)
                temp++;

            for (int i = 0; i < temp; i++)
            {
                m_cmbTemp.Items.Add(i + 1);
            }
        }

        private void AddComboboxTimTheo()
        {
            cmbTimTheo.Items.Clear();
            cmbTimTheo.Items.Add("Mã Nhóm Sản Phẩm");
            cmbTimTheo.Items.Add("Tên Sản Phẩm");
            cmbTimTheo.Items.Add("Loại Sản Phẩm");
            cmbTimTheo.Items.Add("Nhà Phân Phối");
            cmbTimTheo.Items.Add("Giá Bán");
            cmbTimTheo.Items.Add("Màu Sắc");
            cmbTimTheo.Items.Add("Kích Thước");
        }

        private void cmdPagingTraCuu_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelectIndex)
            {
                int temp = int.Parse(cmdPagingTraCuu.SelectedItem.ToString()) - 1;
                if (cmbTimTheo.SelectedItem == null)
                {
                    int sumrows = selectDB.SelectAllDSSanPham().Count;
                    if (m_iTo + m_iDistance * temp > sumrows)
                        dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham(m_iFrom + m_iDistance * temp, sumrows - 1);
                    else
                        dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham(m_iFrom + m_iDistance * temp, m_iTo + m_iDistance * temp);
                }
                else
                {
                    string m_sSelect = cmbTimTheo.SelectedItem.ToString();
                    switch (m_sSelect)
                    {
                        case "Mã Sản Phẩm":
                            break;
                        case "Loại Sản Phẩm":
                            {
                                int sumrows = selectDB.SelectAllDSSanPham_LoaiSP(txtTimTheo.Text).Count;
                                if (m_iTo + m_iDistance * temp > sumrows)
                                    dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_LoaiSP(txtTimTheo.Text, m_iFrom + m_iDistance * temp, sumrows - 1);
                                else
                                    dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_LoaiSP(txtTimTheo.Text, m_iFrom + m_iDistance * temp, m_iTo + m_iDistance * temp);
                            }
                            break;
                        case "Giá Bán":
                            {
                                int sumrows = selectDB.SelectAllDSSanPham_GiaBan(int.Parse(txtTimTheo.Text)).Count;
                                if (m_iTo + m_iDistance * temp > sumrows)
                                    dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_GiaBan(int.Parse(txtTimTheo.Text), m_iFrom + m_iDistance * temp, sumrows - 1);
                                else
                                    dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_GiaBan(int.Parse(txtTimTheo.Text), m_iFrom + m_iDistance * temp, m_iTo + m_iDistance * temp);
                            }
                            break;
                        case "Kích Thước":
                            {
                                int sumrows = selectDB.SelectAllDSSanPham_KichThuoc(float.Parse(txtTimTheo.Text)).Count;
                                if (m_iTo + m_iDistance * temp > sumrows)
                                    dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_KichThuoc(float.Parse(txtTimTheo.Text), m_iFrom + m_iDistance * temp, sumrows - 1);
                                else
                                    dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_KichThuoc(float.Parse(txtTimTheo.Text), m_iFrom + m_iDistance * temp, m_iTo + m_iDistance * temp);
                            }
                            break;
                        case "Tên Sản Phẩm":
                            {
                                int sumrows = selectDB.SelectAllDSSanPham_TenSP(txtTimTheo.Text).Count;
                                if (m_iTo + m_iDistance * temp > sumrows)
                                    dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_TenSP(txtTimTheo.Text, m_iFrom + m_iDistance * temp, sumrows - 1);
                                else
                                    dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_TenSP(txtTimTheo.Text, m_iFrom + m_iDistance * temp, m_iTo + m_iDistance * temp);
                            }
                            break;
                        case "Nhà Phân Phối":
                            {
                                int sumrows = selectDB.SelectAllDSSanPham_NhaPP(txtTimTheo.Text).Count;
                                if (m_iTo + m_iDistance * temp > sumrows)
                                    dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_NhaPP(txtTimTheo.Text, m_iFrom + m_iDistance * temp, sumrows - 1);
                                else
                                    dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_NhaPP(txtTimTheo.Text, m_iFrom + m_iDistance * temp, m_iTo + m_iDistance * temp);
                            }
                            break;
                        case "Màu Sắc":
                            {
                                int sumrows = selectDB.SelectAllDSSanPham_MauSac(txtTimTheo.Text).Count;
                                if (m_iTo + m_iDistance * temp > sumrows)
                                    dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_MauSac(txtTimTheo.Text, m_iFrom + m_iDistance * temp, sumrows - 1);
                                else
                                    dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_MauSac(txtTimTheo.Text, m_iFrom + m_iDistance * temp, m_iTo + m_iDistance * temp);
                            }
                            break;
                    }
                }
            }
            else
                m_bSelectIndex = false;
        }

        private void bntTimKiem_Click(object sender, RoutedEventArgs e)
        {
            if (!m_bSelectIndex)
                m_bSelectIndex = true;
            cmdPagingTraCuu.Items.Clear();
            if (cmbTimTheo.SelectedItem == null)
                MessageBox.Show("Chưa chọn tìm kiếm theo kiểu gì", "Thông Báo");
            else if (txtTimTheo.Text == "")
                MessageBox.Show("Chưa nhập chuỗi tìm kiếm", "Thông Báo");
            else
            {
                switch (cmbTimTheo.SelectedItem.ToString())
                {
                    case "Mã Nhóm Sản Phẩm":
                        if (selectDB.SelectAllDSSanPham_MaNhomSP(txtTimTheo.Text) == null)
                            MessageBox.Show("Không tìm thấy sản phẩm", "Thông Báo");
                        else
                            dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_MaNhomSP(txtTimTheo.Text);
                        break;
                    case "Loại Sản Phẩm":
                        if (selectDB.SelectAllDSSanPham_LoaiSP(txtTimTheo.Text) == null)
                            MessageBox.Show("Không tìm thấy sản phẩm", "Thông Báo");
                        else
                            {
                                cmdPagingTraCuu.Items.Clear();
                                dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_LoaiSP(txtTimTheo.Text, m_iFrom, m_iTo);
                                AddComboboxPagingTraCuu(cmdPagingTraCuu, selectDB.SelectAllDSSanPham_LoaiSP(txtTimTheo.Text).Count);
                            }
                        break;
                    case "Giá Bán":
                        int m_iValueTemp;
                        bool result1 = int.TryParse(txtTimTheo.Text, out m_iValueTemp);
                        if (result1 == false)
                            MessageBox.Show("Hãy nhập một số nguyên", "Thông Báo");
                        else
                        {
                            if (selectDB.SelectAllDSSanPham_GiaBan(m_iValueTemp) == null)
                                MessageBox.Show("Không tìm thấy sản phẩm", "Thông Báo");
                            else
                            {
                                cmdPagingTraCuu.Items.Clear();
                                dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_GiaBan(m_iValueTemp, m_iFrom, m_iTo);
                                AddComboboxPagingTraCuu(cmdPagingTraCuu, selectDB.SelectAllDSSanPham_GiaBan(m_iValueTemp).Count);
                            }
                        }
                        break;
                    case "Kích Thước":
                        float m_fValueTemp;
                        bool result2 = float.TryParse(txtTimTheo.Text, out m_fValueTemp);
                        if (result2 == false)
                            MessageBox.Show("Hãy nhập một số", "Thông Báo");
                        else
                        {
                            if (selectDB.SelectAllDSSanPham_KichThuoc(m_fValueTemp) == null)
                                MessageBox.Show("Không tìm thấy sản phẩm", "Thông Báo");
                            else
                            {
                                cmdPagingTraCuu.Items.Clear();
                                dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_KichThuoc(m_fValueTemp, m_iFrom, m_iTo);
                                AddComboboxPagingTraCuu(cmdPagingTraCuu, selectDB.SelectAllDSSanPham_KichThuoc(m_fValueTemp).Count);
                            }
                        }
                        break;
                    case "Tên Sản Phẩm":
                        if (selectDB.SelectAllDSSanPham_TenSP(txtTimTheo.Text) == null)
                            MessageBox.Show("Không tìm thấy sản phẩm", "Thông Báo");
                        else
                        {
                            cmdPagingTraCuu.Items.Clear();
                            dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_TenSP(txtTimTheo.Text, m_iFrom, m_iTo);
                            AddComboboxPagingTraCuu(cmdPagingTraCuu, selectDB.SelectAllDSSanPham_TenSP(txtTimTheo.Text).Count);
                        }
                        break;
                    case "Nhà Phân Phối":
                        if (selectDB.SelectAllDSSanPham_NhaPP(txtTimTheo.Text) == null)
                            MessageBox.Show("Không tìm thấy sản phẩm", "Thông Báo");
                        else
                        {
                            cmdPagingTraCuu.Items.Clear();
                            dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_NhaPP(txtTimTheo.Text, m_iFrom, m_iTo);
                            AddComboboxPagingTraCuu(cmdPagingTraCuu, selectDB.SelectAllDSSanPham_NhaPP(txtTimTheo.Text).Count);
                        }
                        break;
                    case "Màu Sắc":
                        if (selectDB.SelectAllDSSanPham_MauSac(txtTimTheo.Text) == null)
                            MessageBox.Show("Không tìm thấy sản phẩm", "Thông Báo");
                        else
                        {
                            cmdPagingTraCuu.Items.Clear();
                            dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham_MauSac(txtTimTheo.Text, m_iFrom, m_iTo);
                            AddComboboxPagingTraCuu(cmdPagingTraCuu, selectDB.SelectAllDSSanPham_MauSac(txtTimTheo.Text).Count);
                        }
                        break;
                }
            }
            m_bSelectIndex = false;
        }

        private void btnRefeshDataDSSanPham_Click(object sender, RoutedEventArgs e)
        {
            m_iDistance = m_iTo - m_iFrom + 1;
            if (m_iFrom + m_iDistance > selectDB.SelectAllDSSanPham().Count)
                dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham();
            else
                dataDSSanPham.ItemsSource = selectDB.SelectAllDSSanPham(m_iFrom, m_iTo);
            cmdPagingTraCuu.SelectedIndex = 0;
        }

        private void cmbTimTheo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txtTimTheo.Clear();
        }
    }
}
